<?php

require('common.php');

if (!empty($G['variable']))
{
	$variable = $G['variable'];
	$table = $G['table'];
	$error = (!is_alphanumeric($variable)) ? 'Please Use Only Letters, Numbers, or Underscores' : '';
	if (!empty($G['domain']))
	{
		$result = sql_exec("SELECT * FROM Variables WHERE User='$user' AND Name='$variable' AND (Domain='$domain' OR Domain IS NULL)");
	} else {
		$result = sql_exec("SELECT * FROM Variables WHERE User='$user' AND Name='$variable' AND User_Table='$table'");
	}
	$error = (mysql_num_rows($result) > 0) ? 'Variable Already Exists' : '';

	// if acceptable table name was entered, save it and redirect memory?domain=domain
	if (empty($error))
	{
		if (!empty($G['domain']))
		{
			sql_exec("INSERT INTO Variables SET User='$user', Domain='$domain', Name='$variable'");
		} else {
			sql_exec("INSERT INTO Variables SET User='$user', User_Table='$table', Name='$variable'");
		}
		header("Location: memory.php?domain=$domain");
	}
}

html_header("add variable");
?>
<body>
<div id="container">

<?php header_div(); ?>

<div id="body">

<h4>add a variable</h4>

<form action="add_variable.php" method="get">

<h2 class="error"><?php echo $error; ?></h2>

<input type="hidden" name="domain" value="<?php echo $G['domain']; ?>" />
<input type="hidden" name="table" value="<?php echo $G['table']; ?>" />

<p>Variable Name: <input name="variable" /></p>
<p><input type="submit" value="save" /></p>

</form>

<?php echo footer(); ?>

</div>



</div>
</body>
</html>
